/***
 * 隐藏Navigation
 */
let hiddenNavigation = function() {
    let that = this
    let resultData = {
        msg: '',
        status: ''
    }
    // 回调执行Promise函数
    return new Promise((resolve, reject) => {
        try {
            that.cjcAuth({
                success(AuthData) {
                    that.cjcLog('auth success')
                    // 统计 或 埋点
                    that.cjcAnalys('hiddenNavigation')
                    that.cjcLog('api/hiddenNavigation start')
                    // 发起请求
                    window.WebViewJavascriptBridge.callHandler('hiddenNavigation', {}, function(response) {
                        if (typeof response === 'string') {
                            response = JSON.parse(response)
                        }
                        resolve(response)
                        console.log(response.msg)
                    })
                },
                fail(data) {
                    that.alert({
                        message: '请登录过后再执行此项操作',
                        title: '警告',
                        btnName: '确定'
                    })
                    that.cjcLog('auth fail')
                    that.cjcLog('hiddenNavigation:' + that.printObject(data))
                }
            })
        } catch (e) {
            // 方法执行失败
            resultData.msg = '设备 异常'
            resultData.status = '3'
            that.cjcLog('hiddenNavigation:' + that.printObject(resultData))
        }
    })
}
/***
 * 显示Navigation
 */
let showNavigation = function() {
    let that = this
    let resultData = {
        msg: '',
        status: ''
    }
    try {
        that.cjcAuth({
            success(AuthData) {
                that.cjcLog('auth success')
                // 统计 或 埋点
                that.cjcAnalys('showNavigation')
                that.cjcLog('showNavigation start')
                // 发起请求
                window.WebViewJavascriptBridge.callHandler('showNavigation', {}, function(response) {
                    if (typeof response === 'string') {
                        response = JSON.parse(response)
                    }
                    console.log(response.msg)
                })
            },
            fail(data) {
                that.alert({
                    message: '请登录过后再执行此项操作',
                    title: '警告',
                    btnName: '确定'
                })
                that.cjcLog('auth fail')
                that.cjcLog('showNavigation:' + that.printObject(data))
            }
        })
    } catch (e) {
        // 方法执行失败
        resultData.msg = '设备 异常'
        resultData.status = '3'
        that.cjcLog('showNavigation:' + that.printObject(resultData))
    }
}
/***
 * 打开新的页面
 * @param obj
 */
let openNewWebView = function(obj) {
    let that = this
    let resultData = {
        msg: '',
        status: ''
    }
    try {
        that.cjcAuth({
            success(AuthData) {
                that.cjcLog('auth success')
                // 统计 或 埋点
                that.cjcAnalys('openNewWebView')
                that.cjcLog('openNewWebView start')
                // 发起请求
                window.WebViewJavascriptBridge.callHandler('openNewWebView', {
                    'url': obj.url,
                    'title': obj.title,
                    'hiddenNavigation': obj.hiddenNavigation
                }, function(response) {
                    if (typeof response === 'string') {
                        response = JSON.parse(response)
                    }
                    console.log(response.msg)
                })
            },
            fail(data) {
                that.cjcLog('auth fail')
                that.alert({
                    message: '请登录过后再执行此项操作',
                    title: '警告',
                    btnName: '确定'
                })
                that.cjcLog('openNewWebView:' + that.printObject(data))
            }
        })
    } catch (e) {
        // 方法执行失败
        resultData.msg = '设备 异常'
        resultData.status = '3'
        that.cjcLog('openNewWebView:' + that.printObject(resultData))
    }
}
/***
 * 关闭页面
 */
let closeWebView = function() {
    let that = this
    let resultData = {
        msg: '',
        status: ''
    }
    try {
        that.cjcAuth({
            success(AuthData) {
                that.cjcLog('auth success')
                // 统计 或 埋点
                that.cjcAnalys('closeWebView')
                that.cjcLog('closeWebView start')
                // 发起请求
                window.WebViewJavascriptBridge.callHandler('closeWebView', {}, function(response) {
                    if (typeof response === 'string') {
                        response = JSON.parse(response)
                    }
                    console.log(response.msg)
                })
            },
            fail(data) {
                that.alert({
                    message: '请登录过后再执行此项操作',
                    title: '警告',
                    btnName: '确定'
                })
                that.cjcLog('auth fail')
                that.cjcLog('closeWebView:' + that.printObject(data))
            }
        })
    } catch (e) {
        // 方法执行失败
        resultData.msg = '设备 异常'
        resultData.status = '3'
        that.cjcLog('closeWebView:' + that.printObject(resultData))
    }
}
/***
 * 刷新页面
 */
let reload = function(obj) {
    let that = this
    let resultData = {
        msg: '',
        status: ''
    }
    try {
        that.cjcAuth({
            success(AuthData) {
                that.cjcLog('auth success')
                // 统计 或 埋点
                that.cjcAnalys('webViewReset')
                that.cjcLog('api/webViewReset start')
                // 发起请求
                window.WebViewJavascriptBridge.callHandler('webViewReset', {}, function(response) {
                    if (typeof response === 'string') {
                        response = JSON.parse(response)
                    }
                    console.log(response.msg)
                })
            },
            fail(data) {
                that.alert({
                    message: '请登录过后再执行此项操作',
                    title: '警告',
                    btnName: '确定'
                })
                that.cjcLog('auth fail')
                that.cjcLog('webViewReset:' + that.printObject(data))
            }
        })
    } catch (e) {
        // 方法执行失败
        resultData.msg = '设备 异常'
        resultData.status = '3'
        that.cjcLog('webViewReset:' + that.printObject(resultData))
    }
}
/***
 * 改变导航栏标题
 * @param title
 */
let modifyNavigationTitle = function(obj) {
    let that = this
    let resultData = {
        msg: '',
        status: ''
    }
    try {
        that.cjcAuth({
            success(AuthData) {
                that.cjcLog('auth success')
                // 统计 或 埋点
                that.cjcAnalys('modifyNavigationTitle')
                that.cjcLog('api/modifyNavigationTitle start')
                // 发起请求
                window.WebViewJavascriptBridge.callHandler('modifyNavigationTitle', {
                    'title': obj.title
                }, function(response) {
                    if (typeof response === 'string') {
                        response = JSON.parse(response)
                    }
                    console.log(response.msg)
                })
            },
            fail(data) {
                that.alert({
                    message: '请登录过后再执行此项操作',
                    title: '警告',
                    btnName: '确定'
                })
                that.cjcLog('auth fail')
                that.cjcLog('modifyNavigationTitle:' + that.printObject(data))
            }
        })
    } catch (e) {
        // 方法执行失败
        resultData.msg = '设备 异常'
        resultData.status = '3'
        that.cjcLog('modifyNavigationTitle:' + that.printObject(resultData))
    }
}
/***
 * 加载本页api
 * @param obj 最大的原始对象
 */
let loadPageApi = function(obj) {
    obj.hiddenNavigation = hiddenNavigation
    obj.showNavigation = showNavigation
    obj.openNewWebView = openNewWebView
    obj.closeWebView = closeWebView
    obj.reload = reload
    obj.modifyNavigationTitle = modifyNavigationTitle
}

export {
    loadPageApi
}
